package com.ruoyi.train.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.train.domain.ExamApplicationStudent;
import com.ruoyi.train.domain.StudentInformation;
import com.ruoyi.train.domain.StudentProcessRecord;
import com.ruoyi.train.domain.TrainPlan;
import com.ruoyi.train.domain.Vo.StudentInformationVo;
import com.ruoyi.train.mapper.*;
import com.ruoyi.train.service.*;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 学员信息Service业务层处理
 * 
 * @author wusihao
 * @date 2023-03-31
 */
@Service
public class examStudentInformationServiceImpl extends ServiceImpl<examStudentInformationMapper, StudentInformation> implements IexamStudentInformationService
{
    @Autowired
    private examStudentInformationMapper examStudentInformationMapper;

    @Autowired
    private ExamApplicationStudentMapper examApplicationStudentMapper;

    @Autowired
    private StudentInformationMapper studentInformationMapper;

    @Autowired
    private IStudentProcessRecordService studentProcessRecordService;
    @Autowired
    private StudentProcessRecordMapper studentProcessRecordMapper;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ITrainPlanService trainPlanService;


    /**
     * 获取学生详细信息学员信息
     * 
     * @param id 学员信息主键
     * @return 学员信息
     */
    @Override
    public StudentInformationVo selectexamStudentInformationById(Long id)
    {

        StudentInformation studentInformation = examStudentInformationMapper.selectexamStudentInformationById(id);
        String serialNumber = studentInformation.getSerialNumber();
        QueryWrapper<StudentProcessRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("serial_number", serialNumber);
        StudentProcessRecord studentProcessRecord = studentProcessRecordMapper.selectOne(queryWrapper);
        StudentInformationVo studentInformationVo = new StudentInformationVo();
        BeanUtils.copyProperties(studentInformation, studentInformationVo);
        BeanUtils.copyProperties(studentProcessRecord, studentInformationVo);

        Long trainPlanId = studentProcessRecord.getTrainPlanId();
        TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
        String workKind = trainPlanServiceById.getWorkKind();
        String qualifiedType = trainPlanServiceById.getQualifiedType();
        if (workKind != null && !workKind.equals("")) {
            SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(workKind));
            studentInformationVo.setWorkKind(dictData.getDictLabel());
        }
        if (qualifiedType != null && !qualifiedType.equals("")) {
            SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(qualifiedType));
            studentInformationVo.setQualifiedType(dictData.getDictLabel());
        }
        if (trainPlanServiceById.getOperationType() != null && !trainPlanServiceById.getOperationType().equals("")) {
            SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(trainPlanServiceById.getOperationType()));
            studentInformationVo.setOperationType(dictData.getDictLabel());
        }
        return studentInformationVo;
    }

    /**
     * 查询学员信息列表
     * 
     * @param examStudentInformation 学员信息
     * @return 学员信息
     */
    @Override
    public List<StudentInformation> selectexamStudentInformationList(StudentInformation examStudentInformation)
    {
        return examStudentInformationMapper.selectexamStudentInformationList(examStudentInformation);
    }

    /**
     * 新增学员信息
     * 
     * @param examStudentInformation 学员信息
     * @return 结果
     */
    @Override
    public int insertexamStudentInformation(StudentInformation examStudentInformation)
    {
        examStudentInformation.setCreateTime(DateUtils.getNowDate());
        return examStudentInformationMapper.insertexamStudentInformation(examStudentInformation);
    }

    /**
     * 修改学员信息
     * 
     * @param examStudentInformation 学员信息
     * @return 结果
     */
    @Override
    public int updateexamStudentInformation(StudentInformation examStudentInformation)
    {
        examStudentInformation.setUpdateTime(DateUtils.getNowDate());
        return examStudentInformationMapper.updateexamStudentInformation(examStudentInformation);
    }

    /**
     * 批量删除学员信息
     * 
     * @param ids 需要删除的学员信息主键
     * @return 结果
     */
    @Override
    public int deleteexamStudentInformationByIds(Long[] ids)
    {
        return examStudentInformationMapper.deleteexamStudentInformationByIds(ids);
    }

    /**
     * 删除学员信息信息
     * 
     * @param id 学员信息主键
     * @return 结果
     */
    @Override
    public int deleteexamStudentInformationById(Long id)
    {
        return examStudentInformationMapper.deleteexamStudentInformationById(id);
    }

    // 通过考试申请表id查询学生信息(jingbanren, fuzeren)
    @Override
    public List<StudentInformationVo> getStudentInfoByExamApplicationId(Long applicationId) {
        // 通过考试申请id查找考试申请学员表
        QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
        examApplicationStudentQueryWrapper.eq("exam_application_id", applicationId);
        List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentMapper.selectList(examApplicationStudentQueryWrapper);

        // 返回前端的学生信息
        List<StudentInformationVo> studentInformationVos = new ArrayList<>();

        // 通过考试申请学员表的学员id找到学生信息表
        for (ExamApplicationStudent examApplicationStudent : examApplicationStudents) {
            Long studentId = examApplicationStudent.getStudentId();
            StudentInformation studentInformation = studentInformationMapper.selectById(studentId);
            // 通过学生编号查找学生记录表
            if (studentInformation == null) continue;
            String serialNumber = studentInformation.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq(serialNumber != null, "serial_number", serialNumber);
            studentProcessRecordQueryWrapper.eq("exam_application_is_pass", 1);
            StudentProcessRecord studentProcessRecord = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
            if (studentProcessRecord == null) continue;

            // 将学生信息， 学生记录加入studentInformationVos中
            StudentInformationVo studentInformationVo = new StudentInformationVo();

            if (studentProcessRecord.getExamPlace() != null) studentInformationVo.setExamPlace(studentProcessRecord.getExamPlace());
            BeanUtils.copyProperties(studentProcessRecord, studentInformationVo);
            BeanUtils.copyProperties(studentInformation, studentInformationVo);

            // 通过学员三项岗位记录表得到培训计划id
            Long trainPlanId = studentProcessRecord.getTrainPlanId();
            TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
            if (trainPlanServiceById.getOperationType() != null && !trainPlanServiceById.getOperationType().equals("")) {
                SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(trainPlanServiceById.getOperationType()));
                studentInformationVo.setOperationType(dictData.getDictLabel());
            }


            studentInformationVos.add(studentInformationVo);
        }


        return studentInformationVos;
    }

    @Override
    public int updateshenqingcailiaoxiangshuInformation(StudentInformationVo studentInformationVo) {
        String serialNumber = studentInformationVo.getSerialNumber();
        QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
        studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
        StudentProcessRecord studentProcessRecordServiceOne = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
        studentProcessRecordServiceOne.setApplyDataNumber(studentInformationVo.getApplyDataNumber());
        return studentProcessRecordService.updateStudentProcessRecord(studentProcessRecordServiceOne);
    }

    // 选择学生是否通过考试申请表
    @Override
    public int selectStudentPassExamApplication(Long[] ids) {
        for (int i = 0; i < ids.length; i ++ ) {
            StudentInformation selectById = studentInformationMapper.selectById(ids[i]);
            String serialNumber = selectById.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
            StudentProcessRecord studentProcessRecordServiceOne = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
            studentProcessRecordServiceOne.setExamApplicationIsPass(1);
            studentProcessRecordService.updateById(studentProcessRecordServiceOne);
        }
        return 1;
    }

    // 通过考试申请表id查询所有学生信息(peixunjigou1)
    @Override
    public List<StudentInformationVo> getAllStudentInfoByExamApplicationId(Long applicationId) {
        // 通过考试申请id查找考试申请学员表
        QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
        examApplicationStudentQueryWrapper.eq("exam_application_id", applicationId);
        List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentMapper.selectList(examApplicationStudentQueryWrapper);

        // 返回前端的学生信息
        List<StudentInformationVo> studentInformationVos = new ArrayList<>();

        // 通过考试申请学员表的学员id找到学生信息表
        for (ExamApplicationStudent examApplicationStudent : examApplicationStudents) {
            Long studentId = examApplicationStudent.getStudentId();
            StudentInformation studentInformation = studentInformationMapper.selectById(studentId);
            // 通过学生编号查找学生记录表
            if (studentInformation == null) continue;
            String serialNumber = studentInformation.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq(serialNumber != null, "serial_number", serialNumber);
            StudentProcessRecord studentProcessRecord = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
            if (studentProcessRecord == null) continue;

            // 将学生信息， 学生记录加入studentInformationVos中
            StudentInformationVo studentInformationVo = new StudentInformationVo();

            if (studentProcessRecord.getExamPlace() != null) studentInformationVo.setExamPlace(studentProcessRecord.getExamPlace());
            BeanUtils.copyProperties(studentProcessRecord, studentInformationVo);
            BeanUtils.copyProperties(studentInformation, studentInformationVo);

            Long trainPlanId = studentProcessRecord.getTrainPlanId();
            TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
            if (trainPlanServiceById.getOperationType() != null && !trainPlanServiceById.getOperationType().equals("")) {
                SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(trainPlanServiceById.getOperationType()));
                studentInformationVo.setOperationType(dictData.getDictLabel());
            }

            studentInformationVos.add(studentInformationVo);
        }


        return studentInformationVos;
    }
}
